package by.grsu.hdm;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;

import by.grsu.hdm.devices.MediaCenter;
import by.grsu.hdm.devices.NetworkMonitor;
import by.grsu.hdm.devices.UsbFlashDrive;
import by.grsu.hdm.devices.WashingMachine;
import by.grsu.hdm.exeption.NoDeviceForPlayerException;
import by.grsu.hdm.exeption.NoReproductionFormatException;
import by.grsu.hdm.media.Track;
import by.grsu.hdm.model.IDevice;
import by.grsu.hdm.model.IHomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.reporting.TextReportBuilder;
import by.grsu.hdm.reporting.TrafficReportData;

public class HomeDeviceManagerStarter {

	public static void main(String[] args) {

		PropertyConfigurator.configure("log4j.properties");

		List<IHomeDevice> devices = new ArrayList<IHomeDevice>();

		MediaCenter media = new MediaCenter("MEDIA#132456");
		UsbFlashDrive flash=new UsbFlashDrive("FLASH-4GB");
		//add Track in flash
				Track track=new Track("yesterday", "mp3");
				Track track1=new Track("war", "avi");
				Track track2=new Track("hello", "mp4");
				flash.addTrack(track);flash.addTrack(track1);flash.addTrack(track2);
				media.getUsbPort().connectDevice(flash);
		MediaCenter media1 = new MediaCenter("MEDIA_NO_Flash");

		devices.add(media);
		devices.add(new WashingMachine("WASHING#45"));

		// turn all devices on..
		for (IHomeDevice device : devices) {
			System.out.println(device.turnOn().getMessage());
		}

		// check for connected devices
		for (IDevice device : devices) {
			if (device instanceof IUsbSupport) {
				IUsbPort usb = ((IUsbSupport) device).getUsbPort();
				if (usb.hasConnectedDevice()) {
					IDevice usbDevice = usb.getConnectedDevice();

					if (usbDevice != null)
						System.out.println(usbDevice.getDeviceId()
								+ " is connected to " + device.getDeviceId());
				}
			}
		}

		PlayerStarter ps = new PlayerStarter();

		// noDeviceForPlayerException and NoReproductionFormatException

		try {
			ps.startPlayer(media);
			ps.startPlayer(media1);
		} catch (NoDeviceForPlayerException | NoReproductionFormatException e) {
			// TODO Auto-generated catch block
			Log.log.error(e.getMessage());
		}

		// register devices for network monitoring
		NetworkMonitor monitor = new NetworkMonitor();

		for (IDevice device : devices) {
			if (device instanceof INetworkAvailable) {
				monitor.registerDevice((INetworkAvailable) device);
			}
		}

		TrafficReportData reportData = monitor.getNetworkStatistics();

		TextReportBuilder text = new TextReportBuilder(reportData);
		String report = text.buildReport();

		System.out.println(report);

		// turn all devices off..
		for (IHomeDevice device : devices) {
			System.out.println(device.turnOff().getMessage());
		}
	}

}
